package com.priox.sigar.jsf.converter;

import com.priox.sigar.application.mappers.SociedadMapper;
import com.priox.sigar.client.dto.SociedadDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;

/**
 * User: aglwkrs
 * Date: 30/11/11
 */
@Component
@FacesConverter("com.priox.sigar.jsf.converter.ManagerSociedadDTOConverter")
public class ManagerSociedadDTOConverter implements Converter {

    @Autowired
    protected SociedadMapper sociedadMapper;



    public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException {
        if (StringUtils.isBlank(value) || value.equals("Seleccione")|| value.equals("Todos")) {
            return null;
        }
        return sociedadMapper.findBySociedadId((value));
    }

    public String getAsString(FacesContext context, UIComponent component, Object value) throws ConverterException {
        if (value == null || !(value instanceof SociedadDTO) ) {
            return null;
        }

        return String.valueOf(((SociedadDTO)value).getSociedadId());
    }
}
